/*
 * CreepData - spod database abstraction object
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

class CreepData extends neko.db.Object
{

	// nasty evil hardcoded paths; this is me being lazy and not reading
	// how to set up apache's script environment path
	public static inline var dbinfopath = "/root/";
	public static inline var nekopath = "/root/neko-1.7.0-linux/";

	public static var databaselocale : String;
	public static var databaselogin : String;
	public static var databasepass : String;
	public static var databasename : String;
	
	public var id : Int; // spod hardcodes "id"

	public var username : String;
	public var groupname : String;
	public var gamenumber : Int;
	public var score : Int;
	public var wavenum : Int;
	public var downloads : Int;

	public var datafile : String; // blob maps to string

	// manager always goes last!
	public static var manager = new CreepDataManager();

	public static function initTable(cnx : neko.db.Connection)
	{
		try
		{
		cnx.request("
		CREATE TABLE CreepData (
		    id INT NOT NULL auto_increment,
		    username VARCHAR(64) NOT NULL,
		    groupname VARCHAR(64) NOT NULL,
		    gamenumber INT NOT NULL,
		    score INT NOT NULL,
		    wavenum INT NOT NULL,
		    downloads INT NOT NULL,
		    datafile MEDIUMBLOB NOT NULL,
		    PRIMARY KEY  (id)
		) ENGINE=InnoDB;
		");
		cnx.request("
		CREATE TABLE GameNums (
		    id INT NOT NULL auto_increment,
		    gamenum INT NOT NULL,
		    PRIMARY KEY  (id)
		) ENGINE=InnoDB;
		");
		cnx.request("
		CREATE TABLE Settings (
		    name VARCHAR(32) NOT NULL,
		    value INT NOT NULL
		) ENGINE=InnoDB;
		");
		cnx.request("
		INSERT INTO Settings VALUES ('curgame', 0);
		");
		}
		catch (c : Dynamic) {
			trace(c);
			trace("please drop the tables CreepData, GameNums, and
				   Settings from the database if you wish to 
				   rebuild it.");
			neko.Sys.exit(1);
		}
	}

	public static function loadDbInfo()
	{
		var f = neko.io.File.read(dbinfopath+"dbinfo",false);
		databaselocale = f.readLine();
		databaselogin = f.readLine();
		databasepass = f.readLine();
		databasename = f.readLine();
		f.close();		
		trace("loaded dbinfo");
	}

	public static function addScore(recording : Recording, 
									score : Int, wavenum : Int)
	{

		// add a creepdata

		var newdata = new CreepData();
		
		newdata.username = Encoder.encode(recording.name);
		newdata.groupname = Encoder.encode(recording.group);
		newdata.gamenumber = recording.seed;
		newdata.datafile = haxe.Serializer.run(recording.record);
		newdata.downloads = 0;
		newdata.wavenum = wavenum;
		newdata.score = score;

		newdata.insert();
		
	}

	public static function auditGame(datafile : String, todaysgame : Int)
	{
		var unserial : Recording = 
			haxe.Unserializer.run(datafile);

		if (unserial.seed==todaysgame)
		{
			neko.Sys.command(nekopath+"neko",[nekopath+"game.n", datafile, "store"]);
			// WARNING; if I get 2.14 billion records,
			// I need to clear the database or neko
			// will experience an int overflow		
			neko.Lib.print("OK");
		}
		else
		{
			neko.Lib.print("out of date");
		}
	}
	
}

class CreepDataManager extends neko.db.Manager<CreepData>
{
	public function new()
	{
		super(CreepData);
	}
		
	public function scoreAbove( score : Int ) 
	{
		var objlist = objects(select("score > "+score),true);
	    if (objlist.length>0)
			return false;
		return true;
	}

}
